package com.csgc.camera.biz.other.activity

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.BounceInterpolator

import androidx.appcompat.app.AppCompatActivity
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.splashscreen.SplashScreenViewProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.banmen.ad.mds.views_notload.DoubleSplashAd
import com.bytedance.sdk.openadsdk.CSJSplashAd
import com.csgc.camera.base.BaseActivity
import com.csgc.camera.util.SpUtils
import com.cysd.spsyxj.databinding.ActivitySplashBinding

import com.gyf.immersionbar.ktx.immersionBar
import com.hlyj.http.base.tool.lib_hlyj_base.Constants
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseModuleSpUtils
import com.hlyj.http.base.tool.lib_hlyj_base.utils.JsonUtil
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.hnyyac.ad.mds.views_notload.IAdviewNotLoadSplashListener
import com.ok.common.api.GlobalApi


class HotSplashActivity : BaseActivity() ,IAdViewListener{
    private lateinit var splashAd: DoubleSplashAd
    private val binding: ActivitySplashBinding by lazy { ActivitySplashBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
         initSpalashAd()
        val splashScreen = installSplashScreen()
        immersionBar {
            transparentStatusBar()
            fitsSystemWindows(false)
            statusBarDarkFont(true)
            transparentNavigationBar()
        }


        setContentView(binding.root)

        loadSplash()
        splashScreen.setOnExitAnimationListener { provider ->
            animLogo(provider)
        }
    }

    private fun animLogo(provider: SplashScreenViewProvider) {

//        if (provider.iconView.parent == null){
//            provider.remove()
//            checkPrivacy()
//            return
//        }
        // 获取父容器的高度以确定动画结束的位置
        val parentHeight = (provider.iconView.parent as View).height

        // 获取视图的高度
        val viewHeight: Int = provider.iconView.height
        // 计算视图底部到父容器底部的距离
        val distanceToBottom: Int = parentHeight - provider.iconView.top - viewHeight

        val moveAnim = ObjectAnimator.ofFloat(provider.iconView, View.TRANSLATION_Y, 0f, distanceToBottom.toFloat())
        moveAnim.interpolator = BounceInterpolator()
        moveAnim.duration = 500

        val scaleX = ObjectAnimator.ofFloat(provider.iconView, "scaleX", 1f, 0.35f);
        val scaleY = ObjectAnimator.ofFloat(provider.iconView, "scaleY", 1f, 0.35f);

        AnimatorSet().apply {
            duration = 2000
            play(scaleX).with(scaleY)
            playSequentially(moveAnim)
            doOnEnd {
                provider.remove()


            }
            start()
        }

    }

    override fun onResume() {
        super.onResume()
        if (binding.lottieView != null) {
            binding.lottieView.start()
        }
    }

    @SuppressLint("SuspiciousIndentation")
    private fun loadSplash() {
        //  toast("???????????????")
        Log.e("qin","loadSplash")
        BaseModuleSpUtils.getInstance().putInt(Constants.OCCODE, 11)
        //是否是归因用户加载不同的广告位

        Log.e("SplashActivity","isMonitor==1")
        splashAd.loadDoubleSplashAd(
            primaryAdUnitId = BaseAdConstants.SPLASH_ID_1,
            secondaryAdUnitId = BaseAdConstants.SPLASH_ID_2,
            container = binding.splashContainer,
            timeout = 10000,
            position = 1,
            isHot = true
        )


    }
    private fun initSpalashAd(){
        splashAd = DoubleSplashAd(this, object : IAdviewNotLoadSplashListener {
            override fun onTrigger(adUnitId: String) {
                val logContent = listOf(
                    mapOf(
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TYPE to com.hlyj.http.base.tool.lib_hlyj_base.Constants.TRIGGER,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ,1),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADFORMAT to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SPLASH,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.PLACEMENTID to adUnitId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                //  Log.e("ProductEnvConstance", "onTrigger${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdShow(ad: CSJSplashAd, adUnitId: String, position:Int) {
                Log.e("splash", "onAdShow${position}")
                val logContent = listOf(
                    mapOf(
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TYPE to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SHOW,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ,1),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADFORMAT to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SPLASH,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.PLACEMENTID to adUnitId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE to position,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClick(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TYPE to com.hlyj.http.base.tool.lib_hlyj_base.Constants.CLICK,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ,1),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADFORMAT to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SPLASH,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.PLACEMENTID to adUnitId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE to ocCodee,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClose(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TYPE to com.hlyj.http.base.tool.lib_hlyj_base.Constants.CLOSE,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ,1),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADFORMAT to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SPLASH,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.PLACEMENTID to adUnitId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE to ocCodee,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)

            }

            override fun onError(msg: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TYPE to com.hlyj.http.base.tool.lib_hlyj_base.Constants.FAIL,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ,1),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADFORMAT to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SPLASH,
                        // Constants.PLACEMENTID to splash,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE to ocCodee,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.DATA to JsonUtil.parseStringToJsonWithMsg(msg),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                GlobalApi.sendLog(logContent)

            }

            override fun onSuccess(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TYPE to com.hlyj.http.base.tool.lib_hlyj_base.Constants.LOAD,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.SEQ,1),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            com.hlyj.http.base.tool.lib_hlyj_base.Constants.HARDWAREINFO),
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADFORMAT to com.hlyj.http.base.tool.lib_hlyj_base.Constants.SPLASH,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.PLACEMENTID to adUnitId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.OCCODE to ocCodee,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        com.hlyj.http.base.tool.lib_hlyj_base.Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }



            override fun onSkipped() {

            }

            override fun goMain() {
                AdManager.loadTwoChapinSimultaneously(
                    this@HotSplashActivity,
                    21,
                    this@HotSplashActivity,
                    object : AdLoadListener {
                        override fun onAdLoadFailed(ad: IAdView?) {

                        }
                    })
                finish()
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.lottieView.stop()
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {

    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
}